﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using DTO;
using DataService;

public partial class Admin_RequestDevice : BasePage
{
    #region const field
    public const int PENDING_FILE = 1;
    public const int COMPLETE_FILE = 2;
    public const int CANCEL_FILE = 3;
    public const int DELETE_FILE = 4;
    #endregion

    private DeviceService service = new DeviceService();
    private IList<Device> devices;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (AdminLogin == null)
            Response.Redirect("~/admin/login.aspx");

         //Control ctrPending= (Control)Master.FindControl("lblPending");
         //if (ctrPending != null)
         //{
         //    LoadGrid("", "", null, null, 0, PENDING_FILE);
         //    ((Label)ctrPending).Text = " (Pending: " + Convert.ToString(devices.Count)+")";
         //}

        if (!IsPostBack)
        {
            Inittial();
        }
    }

    private void Inittial()
    {
        rbl_RequestStatus.DataSource = service.GetRequestStatus();
        DataBind();
        if (rbl_RequestStatus.Items.Count > 1)
            rbl_RequestStatus.SelectedIndex = PENDING_FILE;
        
        //gvRequestDevice.DataSource = service.GetListRequestDevice();
        //DataBind();

        LoadGrid("", "", null, null, 0, Convert.ToInt32(rbl_RequestStatus.SelectedValue));
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
        SearchData();
    }

    private void SearchData()
    {
        DateTime temp;
        DateTime? dtFromDate = null, dtToDate = null;
        if (DateTime.TryParse(FromDate.Text, out temp))
        {
            dtFromDate = temp;
        }
        if (DateTime.TryParse(ToDate.Text, out temp))
        {
            dtToDate = temp;
        }

        LoadGrid(txtAccountName.Text.Trim(), txtUserName.Text.Trim(), dtFromDate, dtToDate, 0, Convert.ToInt32(rbl_RequestStatus.SelectedValue));
    }

    protected void rbl_RequestStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        SearchData();
    }

    protected void gvRequestDevice_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lbtDelete = (LinkButton)e.Row.FindControl("lbtDelete");
            HyperLink hplSet = (HyperLink)e.Row.FindControl("hplSet");
            Device device=(Device)e.Row.DataItem;
            switch (device.RequestStatus)
            {
                case PENDING_FILE:
                    lbtDelete.Enabled = true;
                    hplSet.Enabled = true;                    
                    break;
                case COMPLETE_FILE:
                    lbtDelete.Enabled = true;
                    hplSet.Enabled = false;
                    break;
                case CANCEL_FILE:
                    lbtDelete.Enabled = true;
                    hplSet.Enabled = true;
                    break;
                case DELETE_FILE:
                    lbtDelete.Enabled = true;
                    hplSet.Enabled = false;
                    break;
                default:
                    break;
            }

        }
    }

    protected void gvRequestDevice_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int RequestDeviceId = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "Delete":
                service.DeleteRequestDevice(RequestDeviceId);
                SearchData();
                break;
            default:
                break;
        }
    }

    protected void gvRequestDevice_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SearchData();
    }

    private void LoadGrid(string AccountName, string UserName, DateTime? RequestFromDate, DateTime? RequestToDate, int WindowId, int RequestStatusId)
    {
        devices = service.GetListRequestDeviceByAll(AccountName, UserName, RequestFromDate, RequestToDate, WindowId, RequestStatusId);
        gvRequestDevice.DataSource = devices;   
        
        DataBind();
    }
}